package com.ling.pl.security.dao;

import com.ling.pl.core.commons.support.DaoPage;
import com.ling.pl.core.commons.utils.AssertHelper;
import com.ling.pl.core.commons.utils.ViewUtils;
import com.ling.pl.security.model.DefaultPosition;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bo.wang
 */
@Component(DefaultPositionDao.BEAN_ID)
public class DefaultPositionDao extends BaseDefaultPositionDao {
    public DefaultPosition newPositionInstance(String positionId) {
        return new DefaultPosition(positionId);
    }

    public List<DefaultPosition> loadUserPositions(String username) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("select p FROM UserPosition up ,DefaultPosition p where up.positionId = p.id and up.username=:username");
        values.put("username", username);
        return findBy(query, values);
    }

    public DefaultPosition loadPositionById(String positionId) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("FROM DefaultPosition P WHERE P.id=:id");
        values.put("id", positionId);
        List<DefaultPosition> list = findBy(query, values);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public DaoPage loadPagePositions(Map params, int pageIndex, int pageSize) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from DefaultPosition p WHERE 1=1 ");
        String companyId = ViewUtils.getString(params, "companyId");
        if (AssertHelper.notEmpty(companyId)) {
            query.append(" AND D.companyId=:companyId");
            values.put("companyId", companyId);
        }
        DaoPage result = pageBy(query, values, pageIndex, pageSize);
        return result;
    }

    public List<DefaultPosition> loadPagePositions(Map params) {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from DefaultPosition p WHERE 1=1 ");
        String companyId = ViewUtils.getString(params, "companyId");
        if (AssertHelper.notEmpty(companyId)) {
            query.append(" AND D.companyId=:companyId");
            values.put("companyId", companyId);
        }
        return findBy(query, values);
    }

    public List<DefaultPosition> findAllPosition() {
        StringBuffer query = new StringBuffer();
        Map values = new HashMap<String, Object>();
        query.append("from DefaultPosition p WHERE 1=1 ");
//		if(AssertHelper.notEmpty(companyId)){
//			query.append(" AND D.companyId=:companyId");
//			values.put("companyId", companyId);
//		}
        return findBy(query, values);
    }


    public String uniqueCheck(String id) {
        String hql = "select count(*) from " + DefaultPosition.class.getName() + " d where d.id = :id";
        Map<String, Object> parameterMap = new HashMap<String, Object>();
        parameterMap.put("id", id);
        if (this.queryForInt(hql, parameterMap) > 0) {
            return "岗位ID已存在！";
        } else {
            return null;
        }
    }
}

